<?php

set_time_limit(3600); 
//ignore_user_abort(1); 

$page_url = "[URL]";

// Temporary images folder

$tmp_images_dir = $_SERVER["DOCUMENT_ROOT"]."/tmp/";

// Phpmailer class

require($_SERVER["DOCUMENT_ROOT"]."/class.phpmailer.php");                    

// Log file initialization

$log=fopen("newsletter.log","a+");

// File with e-mails (separated with \n)

$emails = array_unique(file("emails.txt"));

// E-mail properties

$mail = new PHPMailer();

$mail->From     = "from@domain.com";
$mail->FromName = "[Sender]";
$mail->Mailer   = "sendmail";
$mail->Subject  = "[subject]";
$mail->CharSet  = "UTF-8";
$mail->IsHTML(true);                                  // set email format to HTML

// Fetching HTML body from the given URL

$body="";

$ch = curl_init($page_url);

curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$body=curl_exec($ch);                                 

curl_close($ch);

// Get all images references from the HTML document and save them in temp folder

preg_match_all("/src\=\"(.*?)\"/si",$body,$images,PREG_SET_ORDER);

foreach ($images as $match)
 {
 $file = $match[1];
 $file_content = "";

 $tmp = explode(".",$file);
 $extension = $tmp[count($tmp)-1];

 $filename = $tmp[count($tmp)-2];

 $ch = curl_init($page_url.$file);
 curl_setopt($ch, CURLOPT_HEADER, 0);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

 $file_content=curl_exec($ch);

 $f=fopen($tmp_images_dir.$filename.".".$extension,"w+");
 fputs($f,$file_content);
 fclose($f);

 chmod($tmp_images_dir.$filename.".".$extension,0777);

 curl_close($ch);

 $body = str_replace($match[0],"src=\"cid:".$filename."\"",$body);

 echo "Retrieving file ".$tmp_images_dir.$filename.".".$extension."...<br>";

 }                                                                             
 
// Emded all retrieved images into message body

if ($dh = opendir($tmp_images_dir)) 
 {
 while ($image = readdir($dh)) 
  if (preg_match("/\.(gif|jp?g)/si",$image))
   {
   list($name,$extension)=explode(".",$image);
   $type=($extension=="gif")?"image/gif":"image/jpeg";
   $mail->AddEmbeddedImage($tmp_images_dir.$image,$name,$name,"base64",$type);
   echo "Embedding file ".$tmp_images_dir.$image.",".$name.",".$name.",".$type."...<br>";
   }
 closedir($dh);
 }

$counter=0;

foreach ($emails as $email)

{

$main_email=$email;

$mail->Body  = str_replace("{\$email}",$main_email,$body);

$mail->AddAddress($email);
	
if(!$mail->Send())
//if(0==1)
 {
 echo "There has been a mail error sending to ".$main_email." (".$mail->ErrorInfo.")<br>";
 fputs($log,"error ".$main_email." ".$mail->ErrorInfo." ".date("d/m/y h:i:s")."\n");
 }
else
 {
 echo "sended to ".$main_email."<br>";
 fputs($log,"ok ".$main_email." ".date("d/m/y h:i:s")."\n");
 }

// Clear all addresses and attachments for next loop
$mail->ClearAddresses();

$counter++;     

if ($counter % 30 == 0 || $counter==10) 
 {
 echo "$counter records processed so far...<br>";
 ob_flush();
 flush(); 
 sleep(3); 
 }

}

$mail->ClearAttachments();

fclose($log);

?>